﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared;
using Sheego.Service.X400.Job.Shared.BO;
using Sheego.Service.X400.Job.Shared.Processor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl.Processor
{
    class CopyEDIFACTMessageProcessor : IX400MessageProcessor
    {
      
        private string Directory = null;
        private IFileManager FileManager;
        public CopyEDIFACTMessageProcessor(IFileManager fileManager)
        {
            FileManager = fileManager;
        }
        #region IX400MessageProcessor Members

        public void Process(IX400Message Message)
        {
            if (FileManager == null)
                throw new ArgumentNullException("FileManager");

            if (Message == null)
                throw new ArgumentNullException("Message");

            foreach (var MsgPart in Message.MessageParts)
            {
                if (MsgPart.ContentType.MIMEType.Equals(Constants.ContentTypeEdifact))
                {
                    //Write EdifactMessage
                    var Pattern = FileManager.CreateUniquePattern(DateTime.Now);
                    var FileName = Directory + @"\" + string.Format("{0}_{1}.EDI", MsgPart.ContentType.SubType , Pattern);
                    FileManager.WriteFile(FileName, MsgPart.Content);
                }
            }
        }

        public void Config(string Key, string Value)
        {
            if(string.IsNullOrWhiteSpace(Key))
                throw new ArgumentNullException("Key");
             if(string.IsNullOrWhiteSpace(Value))
                throw new ArgumentNullException("Value");

             //Directory
             if (Key.Equals("Directory"))
             {
                 Directory = Value;
                 return;
             }

             throw new KeyNotFoundException(Key);
        }

        #endregion

    }
}
